﻿// // ******************************************************************
// //       /\ /|       @file       CacheKeyCommandHandler.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2025-02-01 16:02
// //    *(__\_\        @Copyright  Copyright (c) , 2025-01-29 21:01 Await
// // ******************************************************************

using AutomaticSubtitles.Contracts.Services;
using AutomaticSubtitles.Service.Application.Cahce.Commands;
using AutomaticSubtitles.Service.DataAccess.Model;
using AutomaticSubtitles.Service.Domain.Config;

namespace AutomaticSubtitles.Service.Application.Cahce;

public class CacheKeyCommandHandler : BaseAutoService
{
    private readonly ICacheKeyRepository _repository;
    private readonly ITranslationService _translationService;

    public CacheKeyCommandHandler(ICacheKeyRepository repository, ITranslationService translationService)
    {
        _repository = repository;
        _translationService = translationService;
    }

    [EventHandler]
    public async Task Handle(UpdateConfigOrderingCommand command)
    {
        var autoCacheKey = await _repository.FindAsync(x => x.Id == command.id);
        ArgumentNullException.ThrowIfNull(autoCacheKey);
        autoCacheKey.Sort = command.sort;
        await _repository.UpdateAsync(autoCacheKey);
        command.Result++;
    }
    
    [EventHandler]
    public async Task Handle(UpdateAiDataCommand command)
    {
        var configCacheKey = await _repository.FindAsync(x => x.Key == CacheCommon.AiConfigValue);
        var config = await _repository.FindAsync(x => x.Key == CacheCommon.AiConfig);
        if (configCacheKey != null)
        {
            configCacheKey.Value = command.model ?? "";
            configCacheKey.SecondaryValue = command.isRun ? "true" : "false";
            await _repository.UpdateAsync(configCacheKey);
        }
        else
        {
            var ai2 = new Auto_CacheKey(BasicsHelper.GetId())
            {
                Name = CacheCommon.AiConfigValue,
                Key = CacheCommon.AiConfigValue,
                SecondaryValue = command.isRun ? "true" : "false",
                Value = command.model ?? "",
                GroupName = "Ai配置",
                IsShow = true,
                CreateTime = DateTime.Now
            };
            await _repository.AddAsync(ai2);
        }

        if (config != null)
        {
            config.SecondaryValue = command.path;
            config.Value = command.value ?? "";
            await _repository.UpdateAsync(config);
        }
        else
        {
            var ai1 = new Auto_CacheKey(BasicsHelper.GetId())
            {
                Name = CacheCommon.AiConfig,
                Key = CacheCommon.AiConfig,
                SecondaryValue = command.path,
                Value = command.value ?? "",
                GroupName = "Ai配置",
                IsShow = true,
                CreateTime = DateTime.Now
            };
            await _repository.AddAsync(ai1);
        }

        await _translationService.InitializeTheConfiguration(command.isRun);
        command.Result = 1;
    }

    [EventHandler]
    public async Task Handle(UpdateModifyPromptWordCommand command)
    {
        var configCacheKey = await _repository.FindAsync(x => x.Key == CacheCommon.AiConfigPromptWord);
        if (configCacheKey != null)
        {
            configCacheKey.Value = command.data;
            await _repository.UpdateAsync(configCacheKey);
        }
        else
        {
            var autoCacheKey = await _repository.AddAsync(new Auto_CacheKey(BasicsHelper.GetId())
            {
                Key = CacheCommon.AiConfigPromptWord,
                Name = CacheCommon.AiConfigPromptWord,
                Value = command.data,
                GroupName = "AI配置",
                IsShow = true
            });
            await _repository.AddAsync(autoCacheKey);
        }

        _translationService.ModifyThePromptWord(command.data);
        command.Result = 1;
    }

    [EventHandler]
    public async Task Handle(AddCacheKeyCommand command)
    {
        var countAsync = await _repository.GetCountAsync(x => x.GroupName == command.CacheKeyVo.groupName);
        if (countAsync > 0)
            throw new Exception("CacheKey已存在");
        await _repository.AddAsync(command.CacheKeyVo.MapTo<Auto_CacheKey>());
        command.Result++;
    }

    [EventHandler]
    public async Task Handle(UpdateCacheKeyCommands command)
    {
        var countAsync = await _repository.FindAsync(x => x.Id == command.CacheKeyVo.id) ?? throw new ArgumentNullException("配置文件不存在！");
        var autoCacheKey = command.CacheKeyVo.MapTo<Auto_CacheKey>();
        autoCacheKey.SetId(countAsync.Id);
        await _repository.UpdateAsync(autoCacheKey);
        command.Result++;
    }

    [EventHandler]
    public async Task Handle(DeleteCacheKeyCommands command)
    {
        var countAsync = await _repository.FindAsync(x => x.Id == command.id) ?? throw new ArgumentNullException("配置文件不存在！");
        await _repository.RemoveAsync(countAsync);
        command.Result++;
    }

    [EventHandler]
    public async Task Handle(UpdateCacheKeyGetMediaDirectoryConfigurationListQuery command)
    {
        if (command.data.filmPath != null)
        {
            var mvCacheKey = await _repository.FindAsync(x => x.Id == command.data.filmPath.id);
            IsDataNotExist(mvCacheKey);
            mvCacheKey.Value = command.data.filmPath.value;
            await _repository.UpdateAsync(mvCacheKey);
        }

        if (command.data.seriesPath != null)
        {
            var mvCacheKey = await _repository.FindAsync(x => x.Id == command.data.seriesPath.id);
            IsDataNotExist(mvCacheKey);
            mvCacheKey.Value = command.data.seriesPath.value;
            await _repository.UpdateAsync(mvCacheKey);
        }

        if (command.data.excludePath != null)
        {
            var mvCacheKey = await _repository.FindAsync(x => x.Id == command.data.excludePath.id);
            IsDataNotExist(mvCacheKey);
            mvCacheKey.Value = command.data.excludePath.value;
            await _repository.UpdateAsync(mvCacheKey);
        }

        command.Result++;
    }

    [EventHandler]
    public async Task Handle(UpdateBaiduTranslationAPIConfigurationCommand command)
    {
        var mvCacheKey = await _repository.FindAsync(x => x.Id == command.data.id);
        IsDataNotExist(mvCacheKey);
        mvCacheKey.Value = command.data.appId;
        mvCacheKey.SecondaryValue = command.data.secretKey;
        await _repository.UpdateAsync(mvCacheKey);

        command.Result++;
    }
}